/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chatNI;

import java.io.*;
import java.net.*;


/* 
 * @author Vansh and deepali
 */
public class ThreadSendMsg extends Thread {

    /**
     * @param msg consists of the message that we want to send
     * @param addr is the address of the user to whom we intent to send the
     * message
     * @param socket It consists of the socket used to send the message
     * @param port is the port for this application
     * @param bool is to tell if the message is to be broadcasted or not
     */
    private byte[] msg;
    private InetAddress addr;
    private DatagramSocket socket;
    private int port;
    private boolean bool;

    /**
     *
     * A thread to send the message as datagram packet is started
     */
    public ThreadSendMsg(byte[] msg, InetAddress addr_dest, int port, boolean broadcast) {
        this.msg = msg;
        this.addr = addr_dest;
        this.port = port;
        this.bool = broadcast;
    }

    public void run() {
        DatagramSocket sendingSocket;
        try {
            DatagramPacket packet = new DatagramPacket(msg, msg.length, addr, port);
            sendingSocket = new DatagramSocket();
            if (bool) {
                sendingSocket.setBroadcast(true);
            }
            sendingSocket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
